// pages/publish/publishAddTask.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
const systemInfo = wx.getWindowInfo();
const bottomInset = Math.abs(systemInfo.safeArea.bottom - systemInfo.windowHeight);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryName: '',
    taskNo: '',
    zongcishu: '10',
    zongtianshu: {},
    taskDurationOptions: [],
    price: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      scrollViewBottomInset: bottomInset + 44,
      bottomInset: bottomInset,
      categoryName: options.categoryName,
      taskNo: options.taskNo,
      price: options.price.substring(1),
    })
    this.requestConfig();
  },
  //获取配置选项信息
  requestConfig() {
    let obj = {
      method: 'GET',
      url: '/task/select-options',
      data: {
        categoryName: this.data.categoryName
      }
    }
    httpUtils.request(obj).then(res => {
      this.setData({
        taskDurationOptions: res.data.taskDurationOptions,
        zongtianshu: res.data.taskDurationOptions[0],
      })
    }).catch(err => {
      console.log(err)
    });
  },
  //任务持续天数
  actionChooseZongtianshu() {
    var options = [];
    this.data.taskDurationOptions.forEach(element => {
      options.push(element.optionDisplay);
    });
    this.setData({
      showPicker: true,
      columns: options
    })
  },
  onClose() {
    this.setData({
      showPicker: false
    });
  },
  onConfirm(event) {
    this.setData({
      zongtianshu: this.data.taskDurationOptions[event.detail.index],
      showPicker: false
    })
  },
    //发布任务
    actionPublishTask() {
      if (this.data.zongcishu < 10) {
        ui.showToast("任务次数不能低于10");
        return false;
      }
      var payPrice = this.data.price*this.data.zongcishu;
      wx.navigateTo({
        url: '/pages/publish/pay/publishPayTask?taskNo='+this.data.taskNo+'&price=￥'+payPrice+'&addDays='+this.data.zongtianshu.optionValue+'&addCount='+this.data.zongcishu,
      })
    }
})